package com.example.whrdemospringboot.Controller;

import com.example.whrdemospringboot.DTO.StudentDTO;
import com.example.whrdemospringboot.dao.Student;
import com.example.whrdemospringboot.service.Response;
import com.example.whrdemospringboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     *
     * 根据用户的id取出信息
     * @param id
     * @return
     */
    @GetMapping("/student/{id}")
    public Response<StudentDTO> getStudentById(@PathVariable Long id) {
        return Response.newSuccess(studentService.getStudentById(id));
    }

    /**
     *  添加学生信息，这里必须添加的是学生的姓名
     * @param studentDTO
     * @return
     */
    @PostMapping("/student")
    public Response<Long> addStudent(@RequestBody StudentDTO studentDTO) {
        return Response.newSuccess(studentService.addStudent(studentDTO));
    }

    @DeleteMapping("/student/{id}")
    public void deleteStudentById(@PathVariable Long id) {
        studentService.deleteStudentById(id);
    }

    @PutMapping("/student/{id}")
    public Response<StudentDTO> updataStudentById(@PathVariable Long id, @RequestParam(required = false)String name,
                                                  @RequestParam(required = false)String email){
        return Response.newSuccess(studentService.updataStudentById(id,name,email));

    }
}
